class UserModel {
  final int id;
  final String username;
  final String nickname;
  final String email;
  final String? phone;
  final String? avatar;
  final bool isActive;
  final DateTime createdAt;
  final DateTime updatedAt;
  final DateTime? lastLoginAt;
  final String? lastLoginIP;
  final String? remark;
  final List<RoleModel> roles;
  final int? status;

  UserModel({
    required this.id,
    required this.username,
    required this.nickname,
    required this.email,
    this.phone,
    this.avatar,
    this.isActive = true,
    required this.createdAt,
    required this.updatedAt,
    this.lastLoginAt,
    this.lastLoginIP,
    this.remark,
    this.roles = const [],
    this.status,
  });

  factory UserModel.fromJson(Map<String, dynamic> json) {
    return UserModel(
      id: json['ID'] as int? ?? json['id'] as int,
      username: json['username'] as String,
      nickname: json['nickname'] as String? ?? '',
      email: json['email'] as String,
      phone: json['phone'] as String?,
      avatar: json['avatar'] as String?,
      isActive: json['status'] == 1,
      createdAt: DateTime.parse(json['created_at'] as String? ?? json['createdAt'] as String),
      updatedAt: DateTime.parse(json['updated_at'] as String? ?? json['updatedAt'] as String),
      lastLoginAt: json['last_login_at'] != null 
          ? DateTime.parse(json['last_login_at'] as String) 
          : null,
      lastLoginIP: json['last_login_ip'] as String?,
      remark: json['remark'] as String?,
      roles: (json['roles'] as List<dynamic>?)
          ?.map((role) => RoleModel.fromJson(role))
          .toList() ?? [],
      status: json['status'] as int?,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'ID': id,
      'username': username,
      'nickname': nickname,
      'email': email,
      'phone': phone,
      'avatar': avatar,
      'status': status ?? (isActive ? 1 : 0),
      'created_at': createdAt.toIso8601String(),
      'updated_at': updatedAt.toIso8601String(),
      'last_login_at': lastLoginAt?.toIso8601String(),
      'last_login_ip': lastLoginIP,
      'remark': remark,
      'roles': roles.map((role) => role.toJson()).toList(),
    };
  }
}

class RoleModel {
  final int id;
  final String name;
  final String? description;
  final List<PermissionModel> permissions;

  RoleModel({
    required this.id,
    required this.name,
    this.description,
    this.permissions = const [],
  });

  factory RoleModel.fromJson(Map<String, dynamic> json) {
    return RoleModel(
      id: json['id'] as int,
      name: json['name'] as String,
      description: json['description'] as String?,
      permissions: (json['permissions'] as List<dynamic>?)
          ?.map((permission) => PermissionModel.fromJson(permission))
          .toList() ?? [],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'description': description,
      'permissions': permissions.map((permission) => permission.toJson()).toList(),
    };
  }
}

class PermissionModel {
  final int id;
  final String name;
  final String? description;

  PermissionModel({
    required this.id,
    required this.name,
    this.description,
  });

  factory PermissionModel.fromJson(Map<String, dynamic> json) {
    return PermissionModel(
      id: json['id'] as int,
      name: json['name'] as String,
      description: json['description'] as String?,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'description': description,
    };
  }
}